{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "import re\n",
    "from http.client import responses\n",
    "\n",
    "import dotenv\n",
    "import os\n",
    "\n",
    "os.chdir(\"..\")\n",
    "print(os.getcwd())\n",
    "\n",
    "dotenv.load_dotenv()"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import json\n",
    "\n",
    "with open(\"data/合成数据初赛测试集.jsonl\", \"r\", encoding=\"utf-8\") as f:\n",
    "    # 载入jsonl\n",
    "    test_date = [json.loads(line) for line in f]\n",
    "\n",
    "print(\"测试集载入完成: \", len(test_date))"
   ],
   "id": "e7e40732806fefd6",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "tools_list=[api[\"name\"] for q in test_date for api in q[\"apis\"]]\n",
    "len(tools_list),len(set(tools_list))\n",
    "# 绘制数量分箱图\n",
    "import numpy as np\n",
    "# 计算每个API出现的次数\n",
    "api_counts = [tools_list.count(api) for api in set(tools_list)]\n",
    "\n",
    "# 计算箱线图相关的统计信息\n",
    "q1 = np.percentile(api_counts, 25)  # 第25百分位\n",
    "median = np.percentile(api_counts, 50)  # 中位数\n",
    "q3 = np.percentile(api_counts, 75)  # 第75百分位\n",
    "min_value = min(api_counts)  # 最小值\n",
    "max_value = max(api_counts)  # 最大值\n",
    "range_value = max_value - min_value  # 范围\n",
    "\n",
    "# 输出这些统计信息\n",
    "print(f\"最小值: {min_value}\")\n",
    "print(f\"第1四分位数 (Q1): {q1}\")\n",
    "print(f\"中位数: {median}\")\n",
    "print(f\"第3四分位数 (Q3): {q3}\")\n",
    "print(f\"最大值: {max_value}\")\n",
    "print(f\"数据范围: {range_value}\")"
   ],
   "id": "c22d43ed9973691b",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "types=[]\n",
    "for data in test_date:\n",
    "    types.extend([v[\"type\"] for api in data[\"apis\"] for k,v in api[\"parameters\"][\"properties\"].items()])\n",
    "print(set(types))\n",
    "# 计算每种types都有多少个\n",
    "types_counts = [types.count(t) for t in set(types)]\n",
    "print(types_counts)"
   ],
   "id": "447fbd52099b26e6",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import json\n",
    "\n",
    "with open(\"data/合成数据决赛赛题.jsonl\", \"r\", encoding=\"utf-8\") as f:\n",
    "    # 载入jsonl\n",
    "    final_date = [json.loads(line) for line in f]\n",
    "\n",
    "print(\"决赛测试集载入完成: \", len(final_date))"
   ],
   "id": "bb392b564189eca9",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "final_date[1]",
   "id": "80be6c10ce8d0630",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "types=[]\n",
    "for data in final_date:\n",
    "    types.extend([v[\"type\"] for api in data[\"apis\"] for k,v in api[\"parameters\"][\"properties\"].items()])\n",
    "print(set(types))\n",
    "# 计算每种types都有多少个\n",
    "types_counts = [types.count(t) for t in set(types)]\n",
    "print(types_counts)"
   ],
   "id": "e018266d7070eb02",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "for data in final_date[:]:\n",
    "    for api in data[\"apis\"]:\n",
    "        for k,v in api[\"parameters\"][\"properties\"].items():\n",
    "            if v[\"type\"]==\"array\":\n",
    "                print(v)\n"
   ],
   "id": "7ad94f15a6e0a7aa",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "len(final_date)",
   "id": "f2dee9ff287ee2ac",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "for data in final_date[:100]:\n",
    "    print(\"id: \",data[\"id\"])\n",
    "    print(\"\\n\".join(f\"{i+1}: {s}\" for i,s in enumerate(data[\"user_messages\"])))\n",
    "    print(\"-----------------\")"
   ],
   "id": "a21c646c47419319",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "-------",
   "id": "b56f85dd5cb8d0bf"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "test_date[0]",
   "id": "2b2d9500c14d00b7",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "index=56\n",
    "prompt=\"\"\"```json\n",
    "{test_date}\n",
    "```\n",
    "\n",
    "上面这个json，是一个LLM的函数调用的训练样本，我需要更改这个样本，给样本添加难度：\n",
    "1. 可以用更多类型：'float', 'array', 'integer', 'number', 'boolean', 'string'，不能使用列出之外的类型。\n",
    "2. 更复杂的参数提取，参数提取需要推理\n",
    "3. 修改用户查询，至少有一个次用户输入，需要多次调用同一个工具，或者不同工具。\n",
    "4. 用户可能不会在一条问题里进行询问，可能在多条问题里进行追问，补充信息。\n",
    "5. 样本的基本结果不能改变，如 user_messages是一个string的list，每个参数至少都有type，description\n",
    "7. array类型，需要item说明,\n",
    "8. 问题数量不要变\n",
    "\n",
    "\"\"\"\n",
    "print(prompt.format(test_date=test_date[index]))\n",
    "# 吧这问题拿去问 grok3，gpt4.5，claude3.7，gemini 等各种模型，不要用推理模型 轮流问，那么就有150条数据了,每次都要进行json格式检查"
   ],
   "id": "b8f1973c65d48225",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from tqdm import tqdm\n",
    "from util.query_openai_api import LLMClient\n",
    "import re\n",
    "grok_client = LLMClient(\n",
    "        model=os.getenv(\"GROK3_MODEL\"),\n",
    "        api_key=os.getenv(\"GROK3_API_KEY\"),\n",
    "        base_url=os.getenv(\"GROK3_BASE_URL\")\n",
    ")\n",
    "rewrite_dataset=[]\n",
    "with tqdm(range(0, 400)) as t:\n",
    "    for i in t:\n",
    "        response=grok_client.create_chat_completion([{\"role\": \"user\", \"content\": prompt.format(test_date=test_date[i])}])\n",
    "        # print(response[\"content\"],i)\n",
    "        rewrite_dataset.append(re.findall(r\"```json(.*?)```\", response[\"content\"], re.DOTALL)[0])\n",
    "with open(\"res/test\", \"w\", encoding=\"utf-8\") as f:\n",
    "    for line in rewrite_dataset:\n",
    "        f.write(json.loads(json.dumps(line,ensure_ascii=False,indent=4)))\n",
    "        f.write(\",\\n\")"
   ],
   "id": "554034e93cc42b6a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import json\n",
    "with open(\"res/半手工数据集.jsonl\", \"r\", encoding=\"utf-8\") as f:\n",
    "    # 载入jsonl\n",
    "    synthesized_data=json.load(f)\n",
    "len(synthesized_data)"
   ],
   "id": "9c1e15555d106536",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
